<?php
/**
 * 电话号码管理
 * @author ice<iceinto@mallog.com.cn>
 */
class PhoneAction extends CommonAction
{
    public function _initialize()
    {
        $this->assign('nav', 4);
        parent::_initialize();
    }

    public function index()
    {
        $MallModel = D('Mall');
        $MallPhoneModel = D('MallPhone');
        $mallList[] = $MallModel->getMall($this->getMallId());
        foreach ($mallList as $k => $mall) {
            $mallList[$k]['phones'] = $MallPhoneModel->getMallList($mall['id']);
            $mallList[$k]['isAuth'] = $this->getMallAuthorize($mall['id']);
        }
        $this->assign('malls', $mallList);
        $this->display();
    }

    /**
     * Get phone number
     *
     * @author ice<iceinto@mallog.com.cn>
     */
    public function phones()
    {
        $mid = intval(getGet('mid'));
        $fid = intval(getGet('fid'));
        $bid = intval(getGet('bid'));
        if ($mid != $this->getMallId()) {
            $this->ajaxReturn('', null, 0);
        }
        $MallModel = D('Mall');
        $MallPhoneModel = D('MallPhone');
        if ($mid && $fid && !$bid) {
            $brands = $MallModel->getFloorBrandList($fid);
            foreach ($brands as $k => $brand) {
                $brands[$k]['isAuth'] = $this->getMallAuthorize($mid, $fid, $brand['id']);
                $brands[$k]['phones'] = $MallPhoneModel->geFloorList($mid, $fid, $brand['id']);
            }
            $this->assign('brands', $brands);
            $this->assign('fid', $fid);
        } else if ($mid && !$fid && !$bid) {
            $floors = $MallModel->getFloorList($mid);
            foreach ($floors as $k => $floor) {
                $floors[$k]['phones'] = $MallPhoneModel->geFloorList($mid, $floor['id']);
                $floors[$k]['isAuth'] = $this->getMallAuthorize($mid, $floor['id']);
            }
            $this->assign('floors', $floors);
        } else {
            $this->ajaxReturn('当前没有号码', null, 0);
        }
        $this->assign('mid', $mid);
        $this->ajaxReturn($this->fetch(), null, 1);
    }

    /**
     * 添加电话号码
     *
     * @author ice<iceinto@mallog.com.cn>
     */
    public function add()
    {
        if (isGet()) {
            $message = null;
            $status = 0;
            $rs = 0;
            // 获取传递的参数信息
            $data = $this->getVals();
            $data ['mid'] = intval(getGet('mid'));
            $data ['fid'] = intval(getGet('fid'));
            $data ['bid'] = intval(getGet('bid'));
            //授权
            $this->ajaxIsAuthorize($data['mid'], $data['fid'], $data['bid']);
            if ($data ['mid'] && !$data ['fid'] && !$data ['bid']) {
                // 执行商场号码添加
            } else if ($data ['mid'] && $data ['fid'] && !$data ['bid']) {
                // 执行商场楼层号码添加
            } else if ($data ['mid'] && $data ['fid'] && $data ['bid']) {
                // 执行添加商场楼层品牌信息
            } else {
                $message = L('_ERROR_MESSAGE_PHONE_ADD');
            }
            if ($message == null) {
                // 执行添加
                if ($data ['name'] && $data ['phone']) {
                    // TODO 添加号码审核
                    $data['editor'] = $this->getLoginUserInfo();
                    $data['add_time'] = time();
                    $data['update_time'] = time();
                    $rs = D('mallPhone')->add($data);
                    if ($rs) {
                        $data ['id'] = M('mallPhone')->getLastInsID();
                        $this->assign('phone', $data);
                        $message = $this->fetch();
                        $status = 1;
                    } else {
                        $message = L('_ERROR_ADD_PHONE');
                    }
                } else {
                    $status = 0;
                }
            }
            $this->ajaxReturn($message, null, $status);
        }
    }

    /**
     * 删除电话号码
     *
     * @author ice<iceinto@mallog.com.cn>
     */
    public function delete()
    {
        $status = 0;
        $MallPhoneModel = M('mallPhone');
        $message = L('_DELETE_LOSE');
        $map ['id'] = intval(getGet('id'));
        $data = $MallPhoneModel->where($map)->find();
        if ($data) {
            //授权
            $this->ajaxIsAuthorize($data['mid'], $data['fid'], $data['bid']);
            $rs = $MallPhoneModel->where($map)->delete();
            if ($rs) {
                $status = 1;
                $message = '';
            }
        } else {
            $message = '号码不存在';
        }
        $this->ajaxReturn($message, null, $status);
    }

    /**
     * 修改电话号码
     *
     * @author ice<iceinto@mallog.com.cn>
     */
    public function update()
    {
        // 执行修改
        $status = 0;
        $message = L('_UPDATE_LOSE');
        $map ['id'] = intval(getGet('id'));
        $phone = M('mallPhone')->where($map)->find();
        //授权
        $this->ajaxIsAuthorize($phone['mid'], $phone['fid'], $phone['bid']);
        if ($phone) {
            $status = 1;
            $this->assign('phone', $phone);
        }
        $data = $this->getVals();
        if ($data ['name'] && $data ['phone']) {
            $map ['id'] = intval(getGet('id'));
            $data['update_time'] = time();
            $rs = D('mallPhone')->where($map)->save($data);
            if ($rs) {
                $status = 1;
                $message = L('_UPDATE_SUCCESS');
            }
            $this->ajaxReturn($message, null, $status);
        } else {
            $this->ajaxReturn($this->fetch(), null, 1);
        }
    }

    /**
     * 获取号码需要的基本信息
     *
     * @author ice<iceinto@mallog.com.cn>
     */
    private function getVals()
    {

        $data ['name'] = hh(getGet('name'));
        $data ['phone'] = hh(getGet('phone'));
        $data ['remark'] = hh(getGet('remark'));
        // TODO 添加对号码 正确性验证
        $data ['remark'] = str_replace('undefined', '', $data ['remark']);
        return $data;
    }
}

?>